package com.yangweiqiao.fastindex;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yangweiqiao.fastindex.view.FastIndex;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private ArrayList<Contact> contacts;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tv = (TextView) findViewById(R.id.tv);


        FastIndex index = (FastIndex) findViewById(R.id.index);

        assert index != null;
        index.setonIndexChangedListener(new FastIndex.onIndexChangedListener() {
            @Override
            public void onSettitng(String s) {

                tv.setVisibility(View.VISIBLE);
                tv.setText(s);

                for (Contact contact : contacts) {
                  if(contact.getFirstLetter().equals(s) ) {

                      int i = contacts.indexOf(contact);
                      listView.setSelection(i);
                      break;
                  }
                }


            }

            @Override
            public void cancle() {
                tv.setVisibility(View.GONE);
            }
        });

        //这里需要联系人的集合  创建联系人的对象
        contacts = new ArrayList<>();
        for (int i = 0; i < Cheeses.NAMES.length; i++) {

            Contact contact = new Contact(Cheeses.NAMES[i], Cheeses.NAMES[i]);
            contacts.add(contact);

        }

        Collections.sort(contacts);
        listView = (ListView) findViewById(R.id.listview);

        listView.setAdapter(new MyAdapter());


    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return contacts.size();
        }

        @Override
        public Object getItem(int position) {
            return contacts.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;

            if (convertView == null) {
                //创建
                view = View.inflate(getApplicationContext(), R.layout.item, null);
            } else {
                //复用
                view = convertView;

            }
            //设置数据
            TextView firstname = (TextView) view.findViewById(R.id.firstname);
            TextView name = (TextView) view.findViewById(R.id.name);

            if (position == 0) {
                firstname.setVisibility(View.VISIBLE);
                firstname.setText(contacts.get(position).getFirstLetter());
            } else {


                if (contacts.get(position - 1).getFirstLetter().equals(contacts.get(position).getFirstLetter())) {
                    firstname.setVisibility(View.GONE);
                } else {
                    firstname.setVisibility(View.VISIBLE);
                    firstname.setText(contacts.get(position).getFirstLetter());

                }
                }

                name.setText(contacts.get(position).getName());

                return view;
            }

        }

    }
